package com.recipedb.recipedb.controller;

import com.recipedb.recipedb.entity.Recipe;
import com.recipedb.recipedb.service.RecipeRecommendationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/recommendations")
@CrossOrigin(origins = "*")
public class RecipeRecommendationController {

    @Autowired
    private RecipeRecommendationService recommendationService;

    @GetMapping("/user/{userId}")
    public ResponseEntity<List<Recipe>> getTop5Recommendations(@PathVariable String userId) {
        try {
            Long numericUserId = Long.parseLong(userId);
            List<Recipe> recommendations = recommendationService.recommendTop5Recipes(numericUserId);
            return ResponseEntity.ok(recommendations);
        } catch (NumberFormatException e) {
            return ResponseEntity.badRequest().body(null);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    @GetMapping("/discover/{userId}")
    public ResponseEntity<List<Recipe>> discoverRecipes(@PathVariable String userId) {
        try {
            Long numericUserId = Long.parseLong(userId);
            List<Recipe> recommendations = recommendationService.Discover5Recipe(numericUserId);
            return ResponseEntity.ok(recommendations);
        } catch (NumberFormatException e) {
            return ResponseEntity.badRequest().body(null);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }
}